<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="number">
      <el-input v-model="dataForm.number" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="隶属科目" prop="parentName">
      <el-input v-model="dataForm.parentName" placeholder="隶属科目" readonly>
        <el-button slot="append" @click="checkParent">选择</el-button>
      </el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck">确定</el-button>
    </span>
    <!-- 弹窗, 隶属科目查询 -->
    <parent-choose v-if="parentVisible" ref="parentDialog" @refreshData="getParentData"></parent-choose>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/eims/subject'
    import ParentChoose from './parentChoose'
    import {mapGetters} from 'vuex'
    export default {
    data () {
      return {
				ischeck: false,
        visible: false,
        parentVisible: false,
        dataForm: {
          id: 0,
          parentId: '',
          parentName: '',
          name: '',
          code: '',
          number: '',
          type: '',
          fullId: '',
          fullName: '',
          fullNumber: '',
          status: '',
          memo: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          modifyUser: '',
          createUserName: '',
          orgId: '',
          tenantId: '',
          unitId: '',
          delFlag: ''
        },
        dataRule: {
          parentName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          memo: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      ParentChoose
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false 
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      checkParent(){
        this.parentVisible = true
        this.$nextTick(() => {
          this.$refs.parentDialog.init();
        });
      },
      getParentData(data){
        this.dataForm.parentId = data.id
        this.dataForm.parentName = " "+data.code + " " + data.name
      }
    }
  }
</script>
